package cn.ray.springframework.core.io;

import cn.hutool.core.lang.Assert;

import java.net.MalformedURLException;
import java.net.URL;

public class DefaultResourceLoader implements ResourceLoader{
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location,"location must be not null");
        //对三种资源的处理方式进行了封装，判断location的样式来决定最后使用哪一种资源获取方式
        if(location.startsWith(CLASSPATH_URL_PREFIX)){
            //如果以classPath开头，说明是classpath模式
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        }
        else{
            try{
                URL url=new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                return new FileSystemResource(location);
            }
        }
    }
}
